package com.server.service;

import com.server.meta.ClassMeta;
import com.server.meta.DatabaseCfgMeta;
import com.server.util.Utils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 14-2-13
 * Time: 上午10:26
 * To change this template use File | Settings | File Templates.
 */
public class HibernateCfgGeneratorService {
    /**
     * single
     *
     * @param generatorParams
     */
    public void generate(GeneratorParams generatorParams) {
        if (generatorParams.getPackageName() == null) {
            generatorParams.setPackageName("com." + Utils.getSplitLast(generatorParams.getPath(), "/") + ".model");
        }
        String packageName = generatorParams.getPackageName();
        ClassMeta classMeta = new ClassMeta();
        classMeta.setPackageName(packageName);
        DatabaseCfgMeta databaseCfgMeta = new DatabaseCfgMeta();
        databaseCfgMeta.setDatabaseName( Utils.getSplitLast(generatorParams.getPath(), "/"));
        classMeta.setDatabaseCfgMeta(databaseCfgMeta);
        Map root = new HashMap();
        root.put("bean", classMeta);

        generatorParams.setTemplateName("hibernateCfg");
        generatorParams.setRoot(root);
        generatorParams.setFilename("hibernate.cfg");
        try {
            new GeneratorService().generateXml(generatorParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
